Skip to content

[Docs] Add Developer Guides section#2517

Open
dsikka wants to merge 9 commits intomainfrom
dev-tutorials
Open

[Docs] Add Developer Guides section#2517
dsikka wants to merge 9 commits intomainfrom
dev-tutorials

Conversation

@dsikka
Copy link
Copy Markdown
Collaborator

@dsikka dsikka commented Mar 25, 2026

Summary

Adds a new Developer Guides section to the docs for contributors extending LLM Compressor:

  • Adding a New Modifier — modifier lifecycle (on_initialize, on_start, on_update, on_end, on_finalize), Pydantic parameter declaration, HooksMixin usage, and a concrete WeightClampModifier walkthrough
  • Adding a New Observer — clarifies that observers compute min/max values (not scale/zero_point directly), explains how compressed-tensors calculate_qparams and generate_gparam consume those values, covers the Observer contract, stateful observers, and a PercentileObserver example with recipe usage
  • Adding MoE Calibration Support for a New Model — explains why MoEs need special calibration handling, the MoECalibrationModule contract, and a step-by-step guide using SequentialLlama4TextMoe as the canonical reference. Covers Llama4-specific patterns: packed expert unpacking via a helper ModuleList, is_permanent=True for vLLM compatibility, shared_expert handling, and applying routing scores to expert outputs (not inputs) to avoid NaNs during calibration

Also updates .nav.yml to add the new section between User Guides and Examples.

Updates the existing User Guides:

  • Observers (guides/observers.md) — corrects observer descriptions (min/max values, not scale/zero_point), fixes base class method names, documents all previously undocumented observer variants (memoryless_minmax, static_minmax, memoryless_mse), and splits observer_kwargs table by observer type with accurate defaults

🤖 Generated with Claude Code

dsikka and others added 2 commits March 25, 2026 15:25
Adds a new Developer Tutorials section with three tutorials:
- Adding a New Modifier: covers the modifier lifecycle, Pydantic params,
  HooksMixin, and a concrete WeightClampModifier example
- Adding a New Observer: covers the Observer contract, stateful observers,
  and a PercentileObserver example with recipe usage
- Adding MoE Model Support: covers MoECalibrationModule, the calibration
  forward pattern, and a step-by-step guide referencing the GLM-4.7 example

Also adds the section to .nav.yml between User Guides and Examples.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review.

Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the project's documentation by adding a dedicated "Developer Guides" section. This new section provides essential resources for contributors looking to extend the LLM Compressor framework, covering key areas such as implementing custom modifiers, creating new observers for quantization, and integrating support for Mixture of Experts (MoE) models. The guides offer in-depth explanations of core contracts, lifecycles, and practical examples to facilitate easier and more effective contributions.

Highlights

  • New Developer Guides Section: A new "Developer Guides" section has been added to the documentation, aimed at contributors extending LLM Compressor.
  • Adding a New Modifier Guide: Detailed guide on the modifier contract, lifecycle (on_initialize, on_start, on_update, on_end, on_finalize), Pydantic parameter declaration, HooksMixin usage, and a concrete WeightClampModifier walkthrough.
  • Adding a New Observer Guide: Comprehensive guide on the Observer contract (get_min_max, get_global_min_max), @Observer.register decorator, stateful observers, and a PercentileObserver example with recipe usage.
  • Adding MoE Model Support Guide: Explains the need for special calibration handling for Mixture of Experts (MoE) models, the MoECalibrationModule contract, the calibration forward pattern, and a step-by-step guide referencing the existing GLM-4.7 implementation.
  • Navigation Update: The .nav.yml file was updated to integrate the new "Developer Guides" section between "User Guides" and "Examples".

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

…ew Model"

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces new developer tutorials for LLM Compressor, covering how to add custom Modifiers, Observers, and support for MoE models. The documentation updates include new markdown files and navigation links. Review feedback suggests improving the clarity of lifecycle hook descriptions in the modifier tutorial, defining the ds variable in the MoE example, and optimizing the get_min_max implementation in the observer tutorial for efficiency and robustness.

@mergify mergify bot added the documentation Improvements or additions to documentation label Mar 25, 2026
dsikka and others added 5 commits March 25, 2026 15:43
Replaces the GLM-4.7 reference with the Llama4 implementation as the
canonical example. Updates the forward pattern, contract section, and
step-by-step to reflect Llama4-specific details including packed expert
unpacking, is_permanent=True, shared_expert handling, and router score
application to expert outputs rather than inputs.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…e/zp

Clarifies that observers return min and max values, which are then passed
to calculate_qparams and generate_gparam in compressed-tensors to produce
scale and zero_point. Adds a section explaining how the base class uses
observer output, and updates the example docstring and tips accordingly.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…vers

- Correct: observers compute min/max values, not scale/zero_point directly;
  compressed-tensors calculate_qparams/generate_gparam handle that conversion
- Fix base class description: correct method names (get_min_max,
  get_global_min_max) and accurate description of what the base class handles
- Document all observer variants: memoryless_minmax, static_minmax, minmax,
  memoryless_mse, mse (previously only MinMax and MSE were mentioned)
- Split observer_kwargs table by observer type with accurate defaults

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@dsikka dsikka added the ready When a PR is ready for review label Mar 26, 2026
@dsikka dsikka requested review from HDCharles and kylesayrs March 26, 2026 18:08
@dsikka dsikka enabled auto-merge (squash) March 26, 2026 18:12
Copy link
Copy Markdown
Collaborator

@kylesayrs kylesayrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the add-modifier section. Overall looks good

I think the only question we need to answer is: How much legacy abstraction do we want to expose to the user? Events? Training events like BATCH_START? Does State belong here or somewhere else?

@@ -0,0 +1,177 @@
# Adding a New Modifier

Modifiers are the core extension point in LLM Compressor. Each compression algorithm — GPTQ, AWQ, SmoothQuant, and others — is implemented as a modifier. This tutorial walks through the modifier contract, lifecycle, and how to implement a custom one.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Modifiers are the core extension point in LLM Compressor. Each compression algorithm — GPTQ, AWQ, SmoothQuant, and others — is implemented as a modifier. This tutorial walks through the modifier contract, lifecycle, and how to implement a custom one.
Modifiers are the core extension point in LLM Compressor. Each compression algorithm — GPTQ, AWQ, SmoothQuant, and others — is implemented as a modifier. This tutorial walks through the modifier contract, lifecycle, and how to implement a custom modifier.


## What is a Modifier?

A modifier is a Pydantic model that hooks into the compression pipeline at well-defined lifecycle points. When you call `oneshot`, LLM Compressor:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A modifier is a Pydantic model that hooks into the compression pipeline at well-defined lifecycle points. When you call `oneshot`, LLM Compressor:
A modifier is a subclass of the `Modifier` base class that hooks into the compression pipeline at well-defined lifecycle points. When you call `oneshot`, LLM Compressor will:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that it's a Pydantic model is more of a technical detail, not core to the identity of a modifier.

Comment on lines +9 to +12
1. Instantiates modifiers from the recipe
2. Calls `initialize` on each modifier
3. Runs calibration batches, firing `Event`s that modifiers respond to
4. Calls `finalize` on each modifier
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that the lack of precision here might be misleading. The most precise description would be something like this:

Suggested change
1. Instantiates modifiers from the recipe
2. Calls `initialize` on each modifier
3. Runs calibration batches, firing `Event`s that modifiers respond to
4. Calls `finalize` on each modifier
* Instantiate modifiers from the recipe
* Call `on_initialize` on each modifier
For each pipeline:
* Dispatch the model, then call `on_start` for each modifier in the pipeline
* Calibrate the model with calibration data, triggering the `calibration_epoch_start`, `sequential_epoch_end`, and `calibration_epoch_end` events for each modifier in the pipeline
* Call `on_end` on each modifier in the pipeline
* Call `on_finalize` on each modifier once all pipelines have finished

I think this level of detail would be fine so long as the formatting looks okay

3. Runs calibration batches, firing `Event`s that modifiers respond to
4. Calls `finalize` on each modifier

Modifiers express what they want to do at each stage by overriding lifecycle hooks.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Modifiers express what they want to do at each stage by overriding lifecycle hooks.
Modifiers express what they want to do at each stage by implementing lifecycle hooks.

| Hook | Called by |
|------|-----------|
| `on_initialize` | `CompressionLifecycle.initialize()` |
| `on_event` / `on_start` / `on_update` / `on_end` | `CompressionLifecycle.event()` → `Modifier.update_event()` |
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that CompressionLifecycle events are confusing and not useful as a concept for users. They're this weird middleware between LifecycleCallbacks and modifier event implementations, and something that I hope we can get rid of at some point.


## Example: A Weight-Clamping Modifier

The following modifier clamps the stored weight tensors of all `Linear` layers to a fixed absolute magnitude. It follows the real modifier pattern by handling both pipelines in `on_event`: when using the sequential pipeline it clamps weights layer-by-layer as each subgraph completes (`SEQUENTIAL_EPOCH_END`), and when using the basic pipeline it clamps all weights at once at the end of calibration (`CALIBRATION_EPOCH_END`).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The following modifier clamps the stored weight tensors of all `Linear` layers to a fixed absolute magnitude. It follows the real modifier pattern by handling both pipelines in `on_event`: when using the sequential pipeline it clamps weights layer-by-layer as each subgraph completes (`SEQUENTIAL_EPOCH_END`), and when using the basic pipeline it clamps all weights at once at the end of calibration (`CALIBRATION_EPOCH_END`).
The following modifier clamps the stored weight tensors of all `Linear` layers to a fixed absolute magnitude. When using the sequential pipeline it clamps weights layer-by-layer as each subgraph completes (`SEQUENTIAL_EPOCH_END`), and when using the basic pipeline it clamps all weights at once at the end of calibration (`CALIBRATION_EPOCH_END`).

"""

max_weight_magnitude: float = 1.0
targets: list[str] = ["Linear"]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
targets: list[str] = ["Linear"]
targets: list[str] = field(default_factory=lambda: ["Linear"])

It is bad practice to declare mutable objects as default values

max_weight_magnitude: float = 1.0
targets: list[str] = ["Linear"]
ignore: list[str] = []

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_clamped: set[str] = PrivateAttr(default_factory=set)

f"No modules matched targets={self.targets} ignore={self.ignore}"
)

self._clamped: set[str] = set()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._clamped: set[str] = set()

from llmcompressor import oneshot

model = AutoModelForCausalLM.from_pretrained("your-model")
tokenizer = AutoTokenizer.from_pretrained("your-model")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a matter of preference, I think "your-model" is less distracting than a real model name like "Qwen/Qwen3-0.6B"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ready When a PR is ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants